package edu.geo.containers.geom.impl;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import edu.geo.containers.geom.Feature;

public class Polygon  implements Feature, Serializable
{
	
	public static final int DEFAULT_POLYGON_ID = -1;
	
	private com.vividsolutions.jts.geom.Polygon jtsPolygon; 
	
	private List<Point> points;
	

	int id = DEFAULT_POLYGON_ID;
	
	String strId;
	public void setStrId(String strId) {
		this.strId = strId;
	}

	/**
	 * 
	 */
	private static final long serialVersionUID = -5998050158989365332L;
	
	protected Polygon(com.vividsolutions.jts.geom.Polygon jtsPolygon) {
		this.jtsPolygon = jtsPolygon;
	}
	
	protected Polygon(int id, com.vividsolutions.jts.geom.Polygon jtsPolygon) {
		this.jtsPolygon = jtsPolygon;
		this.id = id;
	}
	

	@Override
	public int getId() {		
		return id;
	}

	@Override
	public void setId(int id) {
		this.id = id;
	}

	@Override
	public String getAsWKT() {
		return jtsPolygon.toText();
	}

	@Override
	public String getAsGML() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public String getAsGeoJson() {
		// TODO Auto-generated method stub
		return null;
	}
	
	public List<Point> getPoints()
	{
		if (points == null)
		{
			points = new ArrayList<Point>();
			for (int i = 0; i < jtsPolygon.getNumPoints(); ++ i)
			{
				points.add(GeometryFactory.createPoint(jtsPolygon.getCoordinates()[i].x,jtsPolygon.getCoordinates()[i].y));
			}
		}
		return points;
	}
	
	public String asSpaghettiRepresentation()
	{
		StringBuffer buffer = new StringBuffer();			
		buffer.append("[");
		int i;
		for (i =0; i < getPoints().size()-1; ++i)
		{
			buffer.append(getPoints().get(i).getId());
			buffer.append(", ");
		}
		buffer.append(getPoints().get(i).getId());
		buffer.append("]");
		return buffer.toString();
	}

	@Override
	public String getStrId() {
		
		return strId;
	}
	
}
